﻿using Ch9RSS_Universal.Data;
using Ch9RSS_Universal.Common;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Windows.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Ch9_Portable.HTMLHelpers;
using Ch9_Portable.Models;
using System.Diagnostics;
using Ch9RSS_Universal.ViewModels;

// The Universal Hub Application project template is documented at http://go.microsoft.com/fwlink/?LinkID=391955

namespace Ch9RSS_Universal
{
    /// <summary>
    /// A page that displays details for a single item within a group.
    /// </summary>
    public sealed partial class ItemPage : Page
    {
        private NavigationHelper navigationHelper;

        public ItemPage()
        {
            this.InitializeComponent();
            this.navigationHelper = new NavigationHelper(this);
            this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
        }

        private ItemViewModel viewModel { get { return (ItemViewModel)this.DataContext; } }

        /// <summary>
        /// Gets the NavigationHelper used to aid in navigation and process lifetime management.
        /// </summary>
        public NavigationHelper NavigationHelper
        {
            get { return this.navigationHelper; }
        }


        private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
        {
            //var item = App.HubVM.GetItemFromGuid((string)e.NavigationParameter);
            var item = (Ch9Item)e.NavigationParameter;
            App.ItemVM.SetCh9Item(item);
            this.DataContext = App.ItemVM;
            if (!App.ItemVM.IsVideoAvailable)
                FirstColumn.Width = new GridLength(0);
            else
                FirstColumn.Width = new GridLength(327, GridUnitType.Star);
            itemContentView.NavigateToString(CSSValues.WinValue + item.description);
            
        }

        #region NavigationHelper registration

        /// <summary>
        /// The methods provided in this section are simply used to allow
        /// NavigationHelper to respond to the page's navigation methods.
        /// Page specific logic should be placed in event handlers for the  
        /// <see cref="Common.NavigationHelper.LoadState"/>
        /// and <see cref="Common.NavigationHelper.SaveState"/>.
        /// The navigation parameter is available in the LoadState method 
        /// in addition to page state preserved during an earlier session.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            this.navigationHelper.OnNavigatedFrom(e);
        }

        #endregion

        private void PushNavigationToIE(WebView sender, WebViewNavigationStartingEventArgs args)
        {
            if (args.Uri != null) 
            {
                args.Cancel = true;
                Windows.System.Launcher.LaunchUriAsync(args.Uri);
            }

        }

        private void sidePlayer_IsFullScreenChanged(object sender, RoutedPropertyChangedEventArgs<bool> e)
        {
            if (e.NewValue) {

                Grid.SetRow(sidePlayer, 0);
                Grid.SetRowSpan(sidePlayer, 3);
                Grid.SetColumnSpan(sidePlayer, 2);
                sidePlayer.Margin = new Thickness(0, 0, 0, 0);
            }
            else
            {
                Grid.SetRow(sidePlayer, 1);
                Grid.SetRowSpan(sidePlayer, 1);
                Grid.SetColumnSpan(sidePlayer, 1);
                sidePlayer.Margin = new Thickness(39, 20, 20, 20);
                //var state = sidePlayer.PlayerState;
                //sidePlayer.Position
                
            }
        }

        private void PauseAllowedChanged(object sender, RoutedEventArgs e)
        {
            var state = sidePlayer.GetMediaState();
            if(sidePlayer.Position.TotalSeconds > 2 &&
                !state.IsPlaying)
            {
                viewModel.SaveViewTime(sidePlayer.Position);
            }
        }

        private void MediaOpened(object sender, RoutedEventArgs e)
        {
            if(viewModel.Ch9Post.LatestViewTime != null &&
                viewModel.Ch9Post.LatestViewTime.TotalSeconds != 0)
            {
                sidePlayer.Position = viewModel.Ch9Post.LatestViewTime;
            }
        }

    }
}